
package com.e885.sample.service;

import java.util.ArrayList;
import java.util.List;

import com.e885.hibernate.HibernateSessionFactory;
import com.e885.log.E885Logger;
import com.e885.sample.exception.QueryException;
import com.e885.sample.model.bean.District;
import com.e885.sample.model.dao.DistrictDAO;
import com.e885.sample.view.bean.common.ComboBoxBean;
import com.e885.service.CommonService;

/**
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class DistrictService extends CommonService
{
	/**
	 * 取得行攻區或鄉鎮市資料 by county
	 * 
	 * @param countyId
	 * @param addZip true, 表示要顯示郵遞區號, 如: 深坑鄉(222)
	 * @return
	 * @throws QueryException
	 */
	public static List< ComboBoxBean > getDistrictListByCounty( String countyId, boolean isAddZip ) throws QueryException
	{
		E885Logger.serviceLog.info( "取得行攻區或鄉鎮市資料 by county:[" + countyId + "]" );
		
		validNull( "countyId", countyId );
		
		try
		{
			List< ComboBoxBean > comboBoxBeanList = new ArrayList< ComboBoxBean >();

			List< District > districtList = new DistrictDAO().findDistrictsByCounty( countyId );

			int size = districtList.size();
			for( int i = 0; i < size; i++ )
			{
				District district = districtList.get( i );

				if( isAddZip )
					comboBoxBeanList.add( new ComboBoxBean( district.getId(), district.getName() + "(" + district.getId() + ")" ) );
				else
					comboBoxBeanList.add( new ComboBoxBean( district.getId(), district.getName() ) );
			}

			return comboBoxBeanList;
		}
		catch( RuntimeException e )
		{
			e.printStackTrace();
			
			E885Logger.serviceLog.error( "無法取得行攻區或鄉鎮市資料 by county:[" + countyId + "]" );
			
			throw new QueryException( "無法取得行攻區或鄉鎮市資料 by county:[" + countyId + "]" );
		}
		finally
		{
			HibernateSessionFactory.closeSession();
		}
	}
}
